const dayjs = require('dayjs')

const filterTimes = (val, format = 'YYYY-MM-DD') => {
  if (isNull(val)) {
    return '--'
  }
  if (val.toString().length === 10) {
    val = parseInt(val) * 1000
  } else if (val.toString().length === 13) {
    val = parseInt(val)
  } else {
    return '--'
  }
  return dayjs(val).format(format)
}

export const isNull = (data) => {
  if (!data) {
    return true
  } else if (JSON.stringify(data) === '{}') {
    return true
  } else if (JSON.stringify(data) === '[]') {
    return true
  }
}

export default (app) => {
  app.config.globalProperties.$filters = {
    filterTimes
  }
}
